<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 显示公司信息 -->
        <treeTools :tree-node="company" :is-root="true" @addDepartemnt="addDepartemnt" />
        <hr>
        <el-tree :data="depts" :props="{label:'name'}" default-expand-all>
          <template #default="obj">
            <treeTools :tree-node="obj.data" @delDepartment="getDepartment" @addDepartemnt="addDepartemnt" @editDepartment="editDepartment" />
          </template>
        </el-tree>
      </el-card>
      <add-dept ref="addDept" :is-show-dialog.sync="isShowDialog" :tree-node="treeNode" @addDepartment="getDepartment" />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDept from './components/add-dept.vue'
import { getDepartment } from '@/api/department'
import { listToTreeData } from '@/utils'
export default {
  components: { treeTools, addDept },
  data() {
    return {
      isShowDialog: false,
      treeNode: {},
      loading: false,
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: []
    }
  },
  async created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      this.loading = true
      const { depts } = await getDepartment()
      this.depts = listToTreeData(depts, '')
      this.loading = false
    },
    addDepartemnt(treeNode) {
      this.isShowDialog = true
      this.treeNode = treeNode
    },
    async editDepartment(treeNode) {
      await this.$refs.addDept.getDepartment(treeNode.id)
      this.treeNode = treeNode
      this.isShowDialog = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>

